import http from './interface'
import store from '@/store'
//设置请求前拦截器
http.interceptor.request = (config) => {
	console.log("====--++++++--"+store.state.userInfo.token)
	config.header = {
		'token': store.state.userInfo.token,
	}

}
//设置请求结束后拦截器
http.interceptor.response = (response) => {
	console.log("=============--------"+store.state.userInfo.token)
	uni.hideLoading()
	if(response.statusCode === 200){
		if ( response.data.code === 20001 || response.data.code === 20002) {
			//未登录
			uni.showToast({
				title: response.data.message,
				duration: 1200,
				icon: 'none'
			})
			setTimeout(function() {
				uni.navigateTo({
					url: '/pages/login/login',
					animationType: 'slide-in-bottom',
					"animationDuration": 450
				});
			}, 450);
			store.commit('logout')
			return
		}
	}else{
		uni.showToast({
			title: '网络好像不太通畅~',
			duration: 1200,
			icon: 'none'
		})
	}

}



/**
 * 将业务所有接口统一起来便于维护
 * 如果项目很大可以将 url 独立成文件，接口分成不同的模块
 *
 */

// 单独导出(测试接口) import {test} from '@/common/vmeitime-http/'
export const test = (data) => {
	/* http.config.baseUrl = "http://localhost:8080/api/"
	//设置请求前拦截器
	http.interceptor.request = (config) => {
		config.header = {
			"token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
		}
	} */
	//设置请求结束后拦截器
	// http.interceptor.response = (response) => {
	// 	console.log('个性化response....')
	// 	//判断返回状态 执行相应操作
	// 	return response;
	// }
	return http.request({
		baseUrl: 'https://unidemo.dcloud.net.cn/',
		url: 'ajax/echo/text?name=uni-app',
		dataType: 'text',
		data,
	})
}

export const getfrontPage = (data, pageNum, pageSize) => {
	http.interceptor.request = (config) => {
		config.header = {
			'pageNum': pageNum,
			'pageSize': pageSize,
			'token':store.state.userInfo.token
		}
	}
	
	return http.request({
		url: 'api/Applets/get_frontPage',
		method: 'POST',
		data
	})
}
export const getUserInfo = (data) => {
	
	return http.request({
		url: 'api/mall/user/get_user_Info',
		method: 'POST',
		data,
	})
}
export const feedback = (data) => {
	
	return http.request({
		url: 'api/Applets/feedback',
		method: 'POST',
		data,
	})
}
export const getAddressList = (data) => {

	return http.request({
		url: 'api/pri/city/getList',
		method: 'POST',
		data,
	})
}
export const addAddress = (data) => {
	uni.showLoading({
		title: '加载中',
		mask: true
	})
	return http.request({
		url: 'api/pri/city/address',
		method: 'POST',
		data,
	})
}

export const delAddress = (data) => {
	uni.showLoading({
		title: '加载中',
		mask: true
	})
	return http.request({
		url: 'api/pri/city/deleteAddress',
		method: 'POST',
		data,
	})
}
export const setDefault = (data) => {

	return http.request({
		url: 'api/pri/city/updateAddress',
		method: 'POST',
		data,
	})
}
export const getAddressInfo = (data) => {

	return http.request({
		url: 'api/pri/city/findObjectById',
		method: 'POST',
		data,
	})
}
export const updateAddress = (data) => {
	uni.showLoading({
		title: '加载中',
		mask: true
	})
	return http.request({
		url: 'api/pri/city/updateAddress',
		method: 'POST',
		data,
	})
}
export const getOrderList = (data, pageNum, pageSize) => {
	http.interceptor.request = (config) => {
		config.header = {
			'pageNum': pageNum,
			'pageSize': pageSize,
			'token':  store.state.userInfo.token
		}
	}

	return http.request({
		url: 'api/orders/getOrdersList',
		method: 'POST',
		data,
	})
}
export const addSubtractProduct = (data) => {
	// uni.showLoading({
	// 	title: '加载中',
	// 	mask: true
	// })

	return http.request({
		url: 'api/shopping_cart/saveData',
		method: 'POST',
		data,
	})
}
export const getCartList = (data) => {


	return http.request({
		url: 'api/shopping_cart/getPage',
		method: 'POST',
		data,
	})
}
export const getProductList = (data,pageNum,pageSize) => {
	http.interceptor.request = (config) => {
		config.header = {
			'pageNum': pageNum,
			'pageSize': pageSize,
			'token': store.state.userInfo.token
		}
	}

	return http.request({
		url: 'api/goods/getPage',
		method: 'POST',
		data,
	})
}
export const clearCart = (data) => {

	return http.request({
		url: 'api/shopping_cart/clearAllByShopId',
		method: 'POST',
		data,
	})
}
export const checkLogin = (data) => {

	return http.request({
		url: 'api/user/checkLogin',
		method: 'POST',
		data,
	})
}
export const openusermember = (data) => {

	return http.request({
		url: 'api/mall/user_member/open_user_member',
		method: 'POST',
		data,
	})
}
export const getuserrelationList = (data) => {

	return http.request({
		url: 'api/mall/user_relation/get_user_relation',
		method: 'POST',
		data,
	})
}
export const getrelation = (data) => {

	return http.request({
		url: 'api/mall/user_relation/get_relation',
		method: 'POST',
		data,
	})
}
export const adduserrelation = (data) => {

	return http.request({
		url: 'api/mall/user_relation/add_user_relation',
		method: 'POST',
		data,
	})
}
export const walletrecorddetail = (data) => {

	return http.request({
		url: 'api/mall/bill/get_user_wallet_record',
		method: 'POST',
		data,
	})
}
export const rechargerecord = (data) => {

	return http.request({
		url: 'api/mall/user_member/get_recharge_record',
		method: 'POST',
		data,
	})
}
export const getQRcode = (data) => {

	return http.request({
		url: 'api/mall/member_recommend/get_QR_code',
		method: 'POST',
		data,
	})
}
export const getrecommendedrecord = (data) => {

	return http.request({
		url: 'api/mall/member_recommend/get_recommended_record',
		method: 'POST',
		data,
	})
}
export const subOrder = (data) => {
	uni.showLoading({
		title: '加载中',
		mask: true
	})

	return http.request({
		url: 'api/orders/addOrderList',
		method: 'POST',
		data,
	})
}

export const payOrder = (data) => {
	uni.showLoading({
		title: '加载中',
		mask: true
	})

	return http.request({
		url: 'api/shopping_cart/getSettlement',
		method: 'POST',
		data,
	})
}
export const getOrdersInfo = (data) => {

	return http.request({
		url: 'api/orders/getOrdersInfo',
		method: 'POST',
		data,
	})
}
export const waybillinformation = (data) => {

	return http.request({
		url: 'api/Applets/get_waybill_information',
		method: 'POST',
		data,
	})
}
export const ordersRefund = (data) => {

	return http.request({
		url: 'api/orders/ordersRefund',
		method: 'POST',
		data,
	})
}
export const refundInfo = (data) => {

	return http.request({
		url: '/api/orders/refundInfo',
		method: 'POST',
		data,
	})
}
export const updateOrderState = (data) => {

	return http.request({
		url: 'api/orders/updateIsAfhalen',
		method: 'POST',
		data,
	})
}
export const waybillnumber = (data) => {

	return http.request({
		url: 'api/Applets/fill_in_the_waybill_number',
		method: 'POST',
		data,
	})
}
export const deleteOrder = (data) => {

	return http.request({
		url: 'api/orders/deleteOrder',
		method: 'POST',
		data,
	})
}
export const getGoodsInfo = (data) => {

	return http.request({
		url: 'api/goods/getGoodsInfo',
		method: 'POST',
		data,
	})
}
export const shopInfo = (data) => {

	return http.request({
		url: 'api/shop/shopInfo',
		method: 'POST',
		data,
	})
}
export default {
	getfrontPage,
	getUserInfo,
	feedback,
	getAddressList,
	addAddress,
	delAddress,
	setDefault,
	getAddressInfo,
	updateAddress,
	getOrderList,
	addSubtractProduct,
	getCartList,
	getProductList,
	clearCart,
	checkLogin,
	openusermember,
	getuserrelationList,
	getrelation,
	adduserrelation,
	walletrecorddetail,
	rechargerecord,
	getQRcode,
	getrecommendedrecord,
	subOrder,
	payOrder,
	getOrdersInfo,
	waybillinformation,
	ordersRefund,
	refundInfo,
	updateOrderState,
	waybillnumber,
	deleteOrder,
	getGoodsInfo,
	shopInfo
}
